package ym.controller.back.website;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import ym.pojo.Feedback;
import ym.service.FeedbackService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 后台反馈
 */
@RestController
@CrossOrigin
@RequestMapping("/back/feedback")
public class FeedbackController {
    @Autowired
    private FeedbackService feedbackService;

    /**
     * 新增
     *
     * @param feedback
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('/website')")
    public BaseResult add(Feedback feedback) {
        feedbackService.add(feedback);
        return BaseResult.ok();
    }

    /**
     * 修改
     *
     * @param feedback
     * @return
     */
    @PostMapping("/update")
    public BaseResult update(Feedback feedback) {
        feedbackService.update(feedback);
        return BaseResult.ok();
    }

    /**
     * 删除
     *
     * @param feedbackId
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer feedbackId) {
        feedbackService.delete(feedbackId);
        return BaseResult.ok();
    }

    /**
     * 根据id查询
     *
     * @param feedbackId
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer feedbackId) {
        Feedback feedback = feedbackService.findById(feedbackId);
        return BaseResult.ok(feedback);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<Feedback> feedbacks = feedbackService.findAll();
        return BaseResult.ok(feedbacks);
    }

    /**
     * 分页查询
     *
     * @param search
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('/website')")
    public BaseResult search(String search, int page, int size) {
        Page<Feedback> feedbackPage = feedbackService.search(search, page, size);
        return BaseResult.ok(feedbackPage);
    }
}
